package com.wwz.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwz.mapper.ChargeMapper;
import com.wwz.pojo.Charge;
import com.wwz.pojo.Personnel;
import com.wwz.pojo.User;
import com.wwz.pojo.query.ChargeQuery;
import com.wwz.util.JobUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 物业收费接口实现
 * @date 2022/03/25 56:12:26
 */
@Service
public class ChargeServiceImpl extends ServiceImpl<ChargeMapper, Charge> implements IChargeService {

    @Resource
    private IPersonnelService personnelService;

    @Resource
    private IUserService userService;


    @Override
    public IPage<Charge> toPage(ChargeQuery query) {
        LambdaQueryWrapper<Charge> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(query.getType())){
            wrapper.like(Charge::getType,query.getType());
        }
        if (StrUtil.isNotEmpty(query.getId())){
            wrapper.eq(Charge::getChargeUser,query.getId());
        }
        wrapper.orderByDesc(Charge::getCreateTime);
        IPage<Charge> page = this.page(query.toPage(), wrapper);

        List<Charge> records = page.getRecords();

        Map<String, List<Personnel>> personnelMap = personnelService.list().stream().collect(Collectors.groupingBy(Personnel::getId));

        Map<String, List<User>> userMap = userService.list().stream().collect(Collectors.groupingBy(User::getId));

        for (Charge record : records) {
            String name = "";
            List<Personnel> personnels = personnelMap.get(record.getChargeUser());
            if (CollUtil.isNotEmpty(personnels)){
                name = personnels.get(0).getName();
            }
            List<User> users = userMap.get(record.getChargeUser());
            if (CollUtil.isNotEmpty(users)){
                name = users.get(0).getUserName();
            }
            record.setChargeUserName(name);
            List<User> houseUser = userMap.get(record.getHouseUser());
            if (CollUtil.isNotEmpty(houseUser)){
                User user = houseUser.get(0);
                record.setHouseUser(user.getUserName());
            }
        }
        return page;
    }

    @Override
    public void addOrUpdate(Charge charge) {
        String chargeName = null;
        String type = null;

        String login = JobUtils.getLogin();
        Charge one = this.getById(charge);

        Personnel personnel = personnelService.getById(login);
        User user = userService.getById(login);

        if (personnel!=null){
            chargeName = personnel.getName();
            type = "0";
        }
        if (user!=null){
            chargeName = user.getUserName();
            type = "1";
        }

        if (one == null){
            charge.setId(StrUtil.uuid());
            charge.setChargeUser(JobUtils.getLogin());
            charge.setChargeUserName(chargeName);
            charge.setCreateTime(new Date());
            charge.setType(type);
            save(charge);
            return;
        }
        charge.setChargeUser(JobUtils.getLogin());
        updateById(charge);
    }



}
